$().ready(function() {
	validateRule();
});


function getCheckedRoles() {
	var adIds = "";
	$("input:checkbox[name=role]:checked").each(function(i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}
function save() {

	// if( !$("#userType").val() ){
	// 	layer.msg('请选择用户类型');
	// 	return false;
	// }

	$("#roleIds").val(getCheckedRoles());
	$.ajax({
		cache : true,
		type : "POST",
		url : "/sys/user/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			username : {
				required : true,
				minlength : 2,
				remote : {
					url : "/sys/user/exist", // 后台处理程序
					type : "post", // 数据发送方式
					dataType : "json", // 接受数据格式
					data : { // 要传递的数据
						username : function() {
							return $("#username").val();
						}
					}
				}
			},
			password : {
				required : true,
				minlength : 6
			},
			// type : {
			// 	required : true
			// },
			confirm_password : {
				required : true,
				minlength : 6,
				equalTo : "#password"
			},
			// email : {
			// 	// required : true,
			// 	email : true
			// },
			mobile : {
				isPhone:true,
				required : true,
				remote : {
					url : "/sys/user/exist", // 后台处理程序
					type : "post", // 数据发送方式
					dataType : "json", // 接受数据格式
					data : { // 要传递的数据
						mobile : function() {
							return $("#mobile").val();
						}
					}
				}
			},
			topic : {
				required : "#newsletter:checked",
				minlength : 2
			},
			agree : "required"
		},
		messages : {

			name : {
				required : icon + "请输入姓名"
			},
			username : {
				required : icon + "请输入您的用户名",
				minlength : icon + "用户名必须两个字符以上",
				remote : icon + "用户名已经存在"
			},
			password : {
				required : icon + "请输入您的密码",
				minlength : icon + "密码必须6个字符以上"
			},
			confirm_password : {
				required : icon + "请再次输入密码",
				minlength : icon + "密码必须6个字符以上",
				equalTo : icon + "两次输入的密码不一致"
			},
			mobile : {
				required : icon + "请输入手机号码",
				remote : icon + "手机号码已经存在"
			}
		}
	})
}

var openDept = function(){
	layer.open({
		type:2,
		title:"选择部门",
		area : [ '300px', '450px' ],
		content:"/sys/dept/treeView"
	})
}
function loadDept( deptId,deptName){
	$("#deptId").val(deptId);
	$("#deptName").val(deptName);
}


$("#userType").change(function (ev){
	if( ev.target.value == 0 ){
		$("#insertion").html(`
			<div class="student">
			  <div class="form-group">
				<label class="col-sm-3 control-label">学历：</label>
				<div class="col-sm-8">
		
				  <select class="form-control valid" id="school_education"  name="education">
					<option value="0">请选择</option>
					<option value="1">本科</option>
					<option value="2">专科</option>
				  </select>
		
				</div>
			  </div>
		
			  <div class="form-group">
				<label class="col-sm-3 control-label">入学时间：</label>
				<div class="col-sm-8">
				  <input id="timeOfEnrollment" name="timeOfEnrollment" class="form-control" type="text">
				</div>
			  </div>
		
			  <div class="form-group">
				<label class="col-sm-3 control-label">学校名称：</label>
				<div class="col-sm-8">
				  <input id="school" name="school" class="form-control" type="text">
				</div>
			  </div>
		
			  <div class="form-group">
				<label class="col-sm-3 control-label">所学专业：</label>
				<div class="col-sm-8">
				  <input id="school_major" name="major" class="form-control" type="text">
				</div>
			  </div>
		
			  <div class="form-group">
				<label class="col-sm-3 control-label">学校所在地：</label>
				<div class="col-sm-8">
				  <input id="schoolAddressOne" name="schoolAddress" class="form-control" type="text">
				</div>
			  </div>
		
			  <div class="form-group">
				<label class="col-sm-3 control-label">学校详细地址：</label>
				<div class="col-sm-8">
				  <input id="schoolIndetailAddressOne" name="schoolIndetailAddress" class="form-control" type="text">
				</div>
			  </div>
		
			  <div class="form-group">
				<label class="col-sm-3 control-label">学校层次：</label>
				<div class="col-sm-8">
		
				  <select class="form-control valid" id="schoolLevel"  name="schoolLevel">
					<option value="0">请选择</option>
					<option value="1">本科</option>
					<option value="2">专科</option>
				  </select>
		
				  <input id="schoolLevelType" class="form-control" type="hidden">
				</div>
			  </div>
		
			  <div class="form-group">
				<label class="col-sm-3 control-label">技能等级证书：</label>
				<div class="col-sm-8">
				  <input id="ratingCertificate" name="ratingCertificate" class="form-control" type="text">
				</div>
			  </div>
		
			</div>
		`)
	}else if( ev.target.value == 1 || ev.target.value == 2 ){
		$("#insertion").html(`
			<div class="teacher">
			
				<div class="form-group">
					<label class="col-sm-3 control-label">身份证号码：</label>
					<div class="col-sm-8">
					  <input id="idNumber" name="idNumber" class="form-control" type="text" maxlength="18">
					</div>
			    </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">政治面貌：</label>
				<div class="col-sm-8">
				  <input id="politicalAffiliation" name="politicalAffiliation" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">单位名称：</label>
				<div class="col-sm-8">
				  <input id="unit_school" name="school" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">单位所在地：</label>
				<div class="col-sm-8">
				  <input id="schoolAddress" name="schoolAddress" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">详细地址：</label>
				<div class="col-sm-8">
				  <input id="schoolIndetailAddress" name="schoolIndetailAddress" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">所在系部：</label>
				<div class="col-sm-8">
				  <input id="department" name="department" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">所在专业：</label>
				<div class="col-sm-8">
				  <input id="major" name="major" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">职位信息：</label>
				<div class="col-sm-8">
				  <input id="officialCapacity" name="officialCapacity" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">职称信息：</label>
				<div class="col-sm-8">
				  <input id="officialCapacityName" name="officialCapacityName" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				  <label class="col-sm-3 control-label">照片信息：</label>
				  <div class="col-sm-8">
					 <input id="img" name="img" class="form-control" type="hidden">
					 <div class="closeBtn">
						
					 </div>
					 <button type="button" class="btn btn-success" id="btnAdd" style="margin-top: 10px;">
						<i class="fa fa-cloud"></i>图片上传
					 </button>
				  </div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">收件地址：</label>
				<div class="col-sm-8">
				  <input id="shippingAddress" name="shippingAddress" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">收件详细地址：</label>
				<div class="col-sm-8">
				  <input id="shippingIndetailAddress" name="shippingIndetailAddress" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">税号：</label>
				<div class="col-sm-8">
				  <input id="dutyParagraph" name="dutyParagraph" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">税号单位名称：</label>
				<div class="col-sm-8">
				  <input id="ticketOrgName" name="ticketOrgName" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">卡号：</label>
				<div class="col-sm-8">
				  <input id="cardNumber" name="cardNumber" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">帐户名：</label>
				<div class="col-sm-8">
				  <input id="accountName" name="accountName" class="form-control" type="text">
				</div>
			  </div>
			
			  <div class="form-group">
				<label class="col-sm-3 control-label">所属银行：</label>
				<div class="col-sm-8">
				  <input id="bank" name="bank" class="form-control" type="text">
				</div>
			  </div>
			
			</div>
		`)
	}

	layui.use(['upload','laydate'], function () {
		var upload = layui.upload, laydate = layui.laydate;

		laydate.render({
			elem: '#timeOfEnrollment'
			,type: 'month'
		});

		upload.render({
			elem: '#btnAdd',
			url: '/common/sysFile/upload',
			accept: 'images', //只允许上传图片
			acceptMime: 'image/*', //只筛选图片
			multiple:true,
			number:5,
			size:10240,
			done: function (r) {

				$(".closeBtn").html(`
					<img id="upImg" src="${r.data}" alt="" width="100" height="100"><span class="iconclose"><img src="../../../img/close.png"></span>
				`);
				$("#img").val(r.data);

				$(".iconclose").click(function (){
					$(".closeBtn").html('');
					$("#img").val('');
				})

			},
			error: function (r) {
				layer.msg(r.msg);
			}
		});
	});

})
